/*
 * @Author: ChunLai
 * @Date: 2022-02-18 10:13:09
 * @LastEditTime: 2025-08-15 10:22:36
 * @Description: 首页配置数据
 * @FilePath: \02.bldinsure\src\store\modules\home.js
 */

const state = {
  hasShare: false, // 分享图标
  hasPlan: false, // 计划书
  hasComputed: true, //保费试算 默认有
  hasSolvency: false,
  mainInsurName: "", // 产品名
  recordName: "", // 备案名
  proCompany: "", // 保司
  insurType: "", // 产品投保类型
  desc: "", // 描述
  bannerImg: "",
  lowerPrice: "",
  wanNengCode: "1037", // 关联的万能险险种编码
  shareInfo: null, // 分享配置信息
  insuDuty: null, // 保障责任
  lipeiText: null, // 理赔: 字符串类型(保司提供的图片); 数据类型(列表文本展示)
  posters: [], // 海报页面背景图
  insuPlan: [], // 保险计划
  productImg: [], // 产品详情图
  // 条款
  apiClauseList: [], // 险种条款(产品特有的)
  tags: null, // 产品简述 / 特点标签
  preInsConfig: null, // 投保前置页配置(后台险种配置,特殊产品才会有)
  // preInsConfig: {
  //   type: "read", // 阅读类 read (比含有readlist) + 问卷类 exam questionnaire (比含有jsonName)
  //   jsonName: "questionnaire", // 获取配置的json名
  //   readlist: [ // 阅读页信息
  //     {
  //       title: "个人信息授权协议",
  //       content: "",
  //       hasTick: "1",
  //       tickText: "我已阅读以上内容并同意授权",
  //     }
  //   ],
  // },
  // pop状态
  noticePop: false,
  planModule: [],
  questions: [], // 问答
  comQuestions: [], // 首页保司公共常见问题
};

const getters = {
  mainInsurName: (state) => state.mainInsurName,
  recordName: (state) => state.recordName,
  planModule: (state) => state.planModule,
  hasPlan: (state) => state.hasPlan,
  hasShare: (state) => state.hasShare,
  hasSolvency: (state) => state.hasSolvency,

  proCompany: (state) => state.proCompany,
  bannerImg: (state) => state.bannerImg,
  lowerPrice: (state) => state.lowerPrice,
  lipeiText: (state) => state.lipeiText,

  insuDuty: (state) => state.insuDuty,
  desc: (state) => state.desc,
  tags: (state) => state.tags,
  productImg: (state) => state.productImg,
  insuPlan: (state) => state.insuPlan,
  questions: (state) => state.questions,
  comQuestions: (state) => state.comQuestions,
  askQues: (state) => {
    let res = [];
    if (state.questions.length > 0) {
      res = [...state.questions];
    }
    if (state.comQuestions.length > 0) {
      if (res.length > 0) {
        state.comQuestions.forEach((e) => {
          const fd2Obj = res.find((r) => r.que == e.que);
          if (!fd2Obj && e.asks) {
            res.push(e);
          }
        });
      } else {
        res = [...state.comQuestions];
      }
    }
    res = res.filter((i) => i.asks);
    return res;
  },
  posters: (state) => state.posters,
  shareInfo: (state) => state.shareInfo,
  baseConfig: (state) => state.baseConfig,
  apiClauseList: (state) => state.apiClauseList,
  noticePop: (state) => state.noticePop,
  hasComputed: (state) => state.hasComputed,
  tags: (state) => state.tags,
  preInsConfig: (state) => state.preInsConfig,
  wanNengCode: (state) => state.wanNengCode,
};

const mutations = {
  updateField(state, payload) {
    Object.assign(state, payload);
  },
  SET_PRE_INSURE(state, payload) {
    state.preInsConfig = payload;
  },
  SET_RECORD_NAME(state, payload) {
    state.recordName = payload;
  },
  SET_COM_QUES(state, payload) {
    state.comQuestions = payload;
  },
  setHomeField(state, payload) {
    // console.log("home/setHomeField", payload)
    for (const key in state) {
      if (Object.prototype.hasOwnProperty.call(payload, key)) {
        const item = payload[key];
        state[key] = item;
      }
    }
  },
};
const actions = {
  /*后台险种配置需要展示的产品条款 投保文档等*/
  setClauseFiles({ state }, data) {
    // console.log('setClauseFiles',data)
    let arr = [];
    const len = data.length;
    for (let i = 0; i < len; i++) {
      const item = data[i];
      if (item.fileList && item.fileList.length > 0) {
        arr = [...arr, ...item.fileList];
      }
    }
    state.apiClauseList = arr;
  },
  /*设置已选险种的条款*/
  // setRiskFiles({ state }, data) {
  //   let arr = [];
  //   const len = data.length;
  //   for (let i = 0; i < len; i++) {
  //     const item = data[i];
  //     if (item.fileList && item.fileList.length > 0) {
  //       arr = [...arr, ...item.fileList];
  //     }
  //   }
  //   arr = arr.filter((item) => item.isShow);
  //   state.riskRuleList = arr;
  // },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
